﻿using Microsoft.AspNetCore.Mvc;
using NetTaste;
using SqlSugar;
using System.Reflection;
using VM.Common.DataUntil;
using VM.Common.Encryption5Until;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.User;
using VM.IBLL.Msg;
using VM.IDAL;

namespace VM.BLL.Msg
{
    public class MsgBLL : BaseBLL,  IMsgBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        public MsgBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
        }

       

        /// <summary>
        /// 校验用户名和密码是否正确
        /// </summary>
        /// <param name="PublicKey"></param>
        /// <param name="PrivateKey"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool CheckUserKeys(string RefreshKey, out string Msg)
        {
            int Countx = _ISqlSuggarDAL.Query<Sys_RefreshToken>(e => e.RefreshKey == RefreshKey
                                                                  && e.EndDateTimes >= DateTime.Now
                                                            ).ToList().Count();
            Msg = Countx.ToString();
            if (Countx >= 1)
            {
                return true;

            }
            else
            {
                return false;
            }


        }

        /// <summary>
        /// 添加RefreshToken值
        /// </summary>
        /// <param name="obj"></param>
        public void AddRefreshKeys(Sys_RefreshToken obj)
        {
            _ISqlSuggarDAL.InsertAsync<Sys_RefreshToken>(obj);
        }

        public JsonResult CheckMsg(sys_message obj)
        {

            return null;
        }
        /// <summary>
        /// 添加验证码到本地
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int  AddMsg(sys_message code)
        {
            //使用注册接口
            var Res = _ISqlSuggarDAL.Insert<sys_message>(code);
            return 1;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PublicKey"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool CheckMsgIsValid(string Phone,   string Code)
        {
             
            long timestamp = Timestamp.getNowTimeStamp();

            int Countx = _ISqlSuggarDAL.Query<sys_message>(e => e.phone == Phone
                                                                && e.endDate >= timestamp
                                                                && e.code == Code
                                                          ).ToList().Count();
            
            if (Countx >= 1)
            {
                return true;

            }
            else
            {
                return false;
            }

        }
    }
}